package com.example.layui_project.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.layui_project.comm.service.Impl.BaseServiceImpl;
import com.example.layui_project.dao.PermissionDao;
import com.example.layui_project.dao.RoleDao;
import com.example.layui_project.dao.UserDao;
import com.example.layui_project.entity.SysPermission;
import com.example.layui_project.entity.SysRole;
import com.example.layui_project.entity.SysUser;
import com.example.layui_project.service.PermissionService;
import com.example.layui_project.service.UserService;
import com.example.layui_project.utils.BestUitls;
import com.example.layui_project.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl extends BaseServiceImpl<SysPermission> implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RoleDao roleDao;
    /*@Override
    public List<SysPermission> getRoleAndPermissionByMenu() {

        return permissionDao.getRoleAndPermissionByMenu();
    }*/

    @Override
    public JSONArray permissionTree(Integer roleId) {
        // 当这roleId不为空的时候说明是根据角色去生成树结构需要设置选中状态
        Map<Object, SysPermission> permissionRoleByMap = null;
        if(BestUitls.isNotBlank(roleId)){
            List<SysRole> permissionByRoleId = roleDao.getPermissionByRoleId(roleId);
            ArrayList<SysPermission> sysPermissions = new ArrayList<>();
            permissionByRoleId.forEach((r)->{
                sysPermissions.add(r.getSysPermissionList().get(0));
            });
            permissionRoleByMap = BestUitls.listToMap(sysPermissions,"getId",SysPermission.class);
        }
        // 第一步：查询出所有的权限(这里应该修改为缓存)
        List<SysPermission> permissions = permissionDao.selectAll();

        JSONArray objects = new JSONArray();
        for (SysPermission per:permissions) {
            /*boolean checked = checked(per, permissionRoleByMap);
            per.setChecked(checked);*/
            if(!BestUitls.isNotBlank(per.getParentId())){
                // 当父ID为空的时候说明当前是父根节点
                SysPermission sysPermission = setPermissionTree2(per, permissions,permissionRoleByMap);
                if(BestUitls.isNotBlank(permissionRoleByMap)){
                    sysPermission = a(sysPermission, permissionRoleByMap);
                }
                objects.add(sysPermission);
            }
        }
        String s = JSONObject.toJSONString(objects);
        System.out.println(s);
        return objects;
    }

    @Override
    public boolean add(SysPermission sysPermission) {
        int i = permissionDao.insertSelective(sysPermission);
        return i > 0 ? true : false;
    }

    @Override
    public boolean del(Integer id) {
        int i = permissionDao.deleteByPrimaryKey(id);
        return i>0 ? true : false;
    }

    public boolean update(SysPermission sysPermission) {
        int i = permissionDao.updateByPrimaryKeySelective(sysPermission);
        return i > 0 ? true : false;
    }

    // 当前遍历的时候我们只遍历二层节点和三层节点
    public SysPermission  setPermissionTree2(SysPermission parentPermission,List<SysPermission> permissions,Map<Object, SysPermission> permissionRoleByMap){
        permissions.forEach((p)->{
            if(p.getParentId()==parentPermission.getId()){
                List<SysPermission> children = parentPermission.getChildren();
                if (!BestUitls.isNotBlank(children) || children.isEmpty()){
                    children = new ArrayList<SysPermission>();
                }
                children.add(p);
                parentPermission.setChildren(children);
                // 并且当前的节点就成为新的父元素
                /*setPermissionTree2(p,permissions,permissionRoleByMap);*/
            }
        });
        return parentPermission;
    }
    public boolean checked(SysPermission sysPermission,Map<Object, SysPermission> permissionRoleByMap){
        return permissionRoleByMap!=null ? BestUitls.isNotBlank(permissionRoleByMap.get(sysPermission.getId())) ? true : false : false;
    }
    public SysPermission a(SysPermission sysPermission,Map<Object, SysPermission> permissionRoleByMap){
        // 判断当前节点是否有子节点如果有自节点，那么获取子节点然后遍历自节点，若有被选中那么就设置选中，若没有子节点就判断当前节点是否被选中
        List<SysPermission> children = sysPermission.getChildren();
        if(BestUitls.isNotBlank(children) && !children.isEmpty()){
            children.forEach((s)->{
                s.setChecked(checked(s,permissionRoleByMap));
            });
        }else{
            sysPermission.setChecked(checked(sysPermission,permissionRoleByMap));
        }
        return sysPermission;
    }
}
